package com.computer.network.security;

import com.computer.network.po.SysUser;
import com.computer.network.service.SysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class CustomerUserDetailsService implements UserDetailsService {
    @Resource
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.findUserByUserName(username);
        //查询失败，抛出异常
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        //查询成功，查询用户所拥有的权限,
        //List<String> stringList = sysUserService.selectUrlListByUserId(sysUser.getId());
        //设置用户权限
        //sysUser.setAuthorityList(stringList);
        // 获取当前用户所具有的角色集合
        List<Long> longList = sysUserService.selectRoleNameByUserId(sysUser.getId());
        sysUser.setRoleId(longList);
        return sysUser;
    }
}
